﻿using MESMian.Context;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESMian.Forms
{
    public partial class ProductsList : Form
    {
        public ProductsList()
        {
            InitializeComponent();
            ucPager1.PagerEvent += UcPager1_PagerEvent;
        }

        private void UcPager1_PagerEvent(int currentPage, int pageSize, out int totalCount)
        {
            using (var mtx = new MyContext())
            {
                var query = from p in mtx.Products
                            orderby p.ProductId ascending
                            join u in mtx.Users on p.MaintainerId equals u.UserId
                            join pc in mtx.ProductCategories on p.ProductUnitId equals pc.ProductCategoryId
                            join ps in mtx.ProductSpecs on p.ProductSpecId equals ps.ProductSpecId
                            join units in mtx.Units on p.ProductUnitId equals units.UintId
                            //-------------------------------------------------------
                            join u1 in mtx.Users on p.CreateUserId equals u1.CreateUserId
                            join u2 in mtx.Users on p.LastUpdateUserId equals u2.LastUpdateUserId
                            select new
                            {
                                p.ProductId,
                                产品名称 = p.ProductName,
                                u.UserId,
                                产品维护人 = u.UserName,
                                维护时间 = p.MaintenanceTime,
                                pc.ProductCategoryId,
                                产品类型 = pc.ProductCategoryName,
                                ps.ProductSpecId,
                                规格型号 = ps.ProductSpecName,
                                units.UintId,
                                单位 = units.UnitName,
                                产品编码 = p.ProductCode,
                                产品图片 = p.ProductImage,
                                状态 = p.Status == 0 ? "正常" : "禁用",
                                创建人 = u1.UserName,
                                创建时间 = p.CreateTime,
                                修改人 = u2 != null ? u2.UserName : null,
                                修改时间 = p.LastUpdateTime
                            };




                totalCount = query.Count();
                int skipNum = (currentPage - 1) * pageSize;
                var data = query.Skip(skipNum).Take(pageSize).ToList();
                dataGridView1.DataSource = data;


            }
        }
    }
}
